
package com.joysuch.wwyt.mh.controller;

import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.mh.bean.MhMonitoringTypeAddBean;
import com.joysuch.wwyt.mh.entity.MhMonitoringType;
import com.joysuch.wwyt.mh.repository.MhMonitoringTypeDao;
import com.joysuch.wwyt.mh.service.MhMonitoringTypeService;
import com.twelvemonkeys.lang.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/mh/monitoring-type")
@Api(tags = "监测点类型")
public class MhMonitoringTypeController {

    @Autowired
    private MhMonitoringTypeService mhMonitoringTypeService;
    @Autowired
    private SimpleSelectListService simpleSelectService;
    @Autowired
    private MhMonitoringTypeDao mhMonitoringTypeDao;

    @GetMapping("/list")
    @ApiOperation(value = "列表展示")
    public ResultBean list(Long categoryId) {
        List<MhMonitoringType> list = mhMonitoringTypeService.list(categoryId);
        return ResultBean.wrapSuccessfulResult(list);
    }

    @GetMapping("simple-list")
    public ResultBean simpleList() {
        List<SimpleSelectListBean> name = simpleSelectService.list(MhMonitoringType.class, "name");
       // removeByName(name, "浓度");
        return ResultBean.success(name);
    }

    @GetMapping("list-name-unit")
    public ResultBean listNameUnit(Long categoryId) {
        List<MhMonitoringType> typeList = mhMonitoringTypeService.list(categoryId);
        List<SimpleSelectListBean> list = new ArrayList<>();
        for (MhMonitoringType type : typeList) {
            SimpleSelectListBean bean = new SimpleSelectListBean();
            if (StringUtil.isEmpty(type.getUnit())) {
                bean.setLabel(type.getName());
            } else {
                bean.setLabel(type.getName() + "(" + type.getUnit() + ")");
            }
            bean.setValue(type.getId());
            list.add(bean);
        }
      //  removeByName(list, "浓度(%LEL)");
        return ResultBean.success(list);
    }

    private void removeByName(List<SimpleSelectListBean> list, String string) {
        Iterator<SimpleSelectListBean> it = list.iterator();
        while (it.hasNext()) {
            SimpleSelectListBean bean = it.next();
            if (string.equals(bean.getLabel())) {
                it.remove();
            }
        }
    }

    @PostMapping("/addType")
    @ApiOperation(value = "新增监测点类型")
    @RequiresPermissions("mh:monitoring-type:addType")
    public ResultBean list(@Validated @RequestBody MhMonitoringTypeAddBean bean) {
        MhMonitoringType saveType = mhMonitoringTypeService.addType(bean);
        return ResultBean.success(saveType);
    }

    @PostMapping("/updateType")
    @ApiOperation(value = "修改监测点类型")
    @RequiresPermissions("mh:monitoring-type:updateType")
    public ResultBean updateType(@Validated @RequestBody MhMonitoringTypeAddBean bean) {
        MhMonitoringType saveType = mhMonitoringTypeService.updateType(bean);
        return ResultBean.success(saveType);
    }

    @GetMapping("/pageType")
    @ApiOperation(value = "监测点类型分页")
    public Object pageType(Pageable page, MhMonitoringTypeAddBean.Search condition) {
        Page<MhMonitoringTypeAddBean> data = mhMonitoringTypeService.pageType(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @GetMapping("/deleteType")
    @ApiOperation(value = "删除监测点类型")
    @RequiresPermissions("mh:monitoring-type:deleteType")
    public ResultBean deleteType(Long id) {
        try {
            mhMonitoringTypeService.deleteType(id);
            return ResultBean.success("");
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode(),e.getMessage());
        }
        
    }

}
